import axios from "axios";
// import {ElMessage} from "element-plus";
import router from '@/router'

// import.meta.env

const service = axios.create({
    baseURL: '/api',
    timeout: 5000,
})

// 请求拦截器
service.interceptors.request.use(config => {
    return config
}, err => {
    return Promise.reject(err)  //返回错误
})

// 响应拦截器
service.interceptors.response.use(res => {
    console.log('---------:', res)
    if (res.status === 200) {
        let {data, config} = res
        if (data.code === undefined) {
            return Promise.resolve(res)
        }
        if (data.code === 200) {
            return Promise.resolve(data)
        } else {
            ElMessage.warning(data.message)
            return Promise.reject(data);
        }
    }
    return Promise.resolve(res.data)
}, err => {
    if (err.code === 'ECONNABORTED' || err.message === "Network Error" || err.message.includes("timeout")) {
        ElMessage.error('请求超时，请稍后重试')
    } else {
        ElMessage.error('系统异常')
    }
    return Promise.reject(err)  //返回错误
})

export default service
